package org.jeecg.modules.tdm.webService;

import cn.hutool.core.codec.Base64;
import org.jeecg.common.util.MinioUtil;
import org.jeecg.modules.tdm.entity.TestFiles;
import org.jeecg.modules.tdm.entity.TestMain;
import org.jeecg.modules.tdm.entity.TestValue;
import org.jeecg.modules.tdm.service.ITestFilesService;
import org.jeecg.modules.tdm.service.ITestMainService;
import org.jeecg.modules.tdm.service.ITestValueService;
import org.jeecg.modules.tdm.utils.DataGridView;
import org.jeecg.modules.tdm.utils.SnowFlake;
import org.jeecg.modules.tdm.utils.TestBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.jws.WebService;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
@WebService(serviceName = "FileWebService", // 与接口中指定的name一致
        targetNamespace = "http://service.business.sxt.com", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "org.jeecg.modules.tdm.webService.FileWebService"// 接口地址
)
public class FileWebServiceImpl implements FileWebService {
    @Autowired
    private ITestMainService testMainService;
    @Autowired
    private ITestFilesService testFilesService;
    @Autowired
    private ITestValueService testValueService;

    @Override
    public String uploadTestData(String xmlString) {
        DataGridView dgv = new DataGridView();
        String resXml="";
        SnowFlake idworker = new SnowFlake();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        //1.解析xml转换成实体
        try {
            List<Map> resultMapList = TestBeanUtils.xmlToBeanList(xmlString);
            for (Map resultMap : resultMapList) {
                String testMainId = idworker.getId();
                TestMain tm = (TestMain) resultMap.get("ITEM");
                tm.setId(testMainId);
                boolean mainFlag = testMainService.save(tm);
                List<TestFiles> fileList = (List<TestFiles>) resultMap.get("FILES");
                //处理file
                if (fileList != null) {
                    for (TestFiles testFile : fileList) {
                        //上传文件
                        byte[] bytes = Base64.decode(testFile.getFileString());
                        InputStream is = new ByteArrayInputStream(bytes);
                        /*
                         * String objectName =
                         * tm.getGC()!=null?tm.getGC()+"/":""+tm.getTEST_ITEM_NO()!=null?tm.
                         * getTEST_ITEM_NO()+"/":""+
                         * tm.getPC()!=null?tm.getPC()+"/":""+tm.getTEST_CONTENT()!=null?tm.
                         * getTEST_CONTENT()+"/":""+ testFile.getFILE_NAME()+"."+testFile.getFILE_EXT();
                         */
                        String path = (tm.getGc() != null ? tm.getGc() + "/" : "") + (tm.getTestItemNo() != null ?
                                tm.getTestItemNo() + "/" : "") + (tm.getPc() != null ? tm.getPc() + "/" : "") +
                                (tm.getTestContent() != null ? tm.getTestContent() : "");
                        String objectName = path + "/" + testFile.getFileName() + "." + testFile.getFileExt();
                        //minio上傳文件

                        MinioUtil.upload(is, objectName);
                        testFile.setId(idworker.getId());
                        testFile.setTestMainId(testMainId);
                        testFile.setFilePath(path);
                        testFile.setFileUrl(MinioUtil.getBucketName()+"/"+objectName);
                        testFile.setFileCreateTime(new Date());
                        testFile.setFileString("");
                    }
                    boolean fileFlag = testFilesService.saveBatch(fileList);
                }
                List<TestValue> valueList = (List<TestValue>) resultMap.get("VALUES");
                if (valueList != null) {
                    for (TestValue testValue : valueList) {
                        //处理value
                        testValue.setTestMainId(testMainId);
                        testValue.setId(idworker.getId());

                    }
                    boolean valueFlag = testValueService.saveBatch(valueList);
                }
            }
            dgv.setCode(200);
            dgv.setMsg("数据上传成功");
            dgv.setCount(null );
            resXml= TestBeanUtils.beanToXml(dgv);
        } catch (Exception e) {
            //TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            // TODO Auto-generated catch block
            e.printStackTrace();
            dgv.setCode(-1);
            dgv.setMsg("数据上传失败:"+e.getMessage());
            dgv.setCount(null);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
            resXml = TestBeanUtils.beanToXml(dgv);
            return resXml;

        }
        return resXml;

    }
}
